﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using AjaxChatService.Controllers;
using AjaxChatService.Models;
using Newtonsoft.Json.Linq;

namespace AjaxChatService.YahooMessenger
{
    public class MessageHelper
    {
        public bool ExecuteSendRequest(string sessionId, string credentials, string content, string targetId)
        {
            //  Create the HTTP URL
            var uri = "http://" + "rcore1.messenger.yahooapis.com" +
                            "/v1/message/yahoo/" + targetId + "?sid=" + sessionId + "&"
                            + credentials;

            WebRequest request = WebRequest.Create(uri);
            request.ContentType = "application/json; charset=utf-8";

            JObject o = new JObject();
            o.Add("message", content);
            string message = o.ToString(); 

            var responseStr = HttpUtils.SendHttpPost(request, null, message);
            //JObject result = JObject.Parse(responseStr);

            //if (result == null)
                //throw new MessengerException(MessengerException.UNKNOWN_SERVER_ERROR, "Unknown server error");

            return true;
        }

        public IEnumerable<YMMessage> ExecuteGetRequest(string sessionId, string credentials, int seq)
        {           
            var uri = "http://" + "rcore1.messenger.yahooapis.com" +
                            "/v1/notifications?sid=" + sessionId + "&seq=" + (seq+1) + "&count=100" + "&"
                            + credentials;

            WebRequest request = WebRequest.Create(uri);
            request.ContentType = "application/json; charset=utf-8";

            var responseStr = HttpUtils.SendHttpGet(request, null);
            JObject result = JObject.Parse(responseStr);

            if (result == null)
                throw new MessengerException(MessengerException.UNKNOWN_SERVER_ERROR, "Unknown server error");

            return UnserializeJSONResponseParameters(result);
        }

        private IEnumerable<YMMessage> UnserializeJSONResponseParameters(JObject result)
        {
            List<YMMessage> lst = new List<YMMessage>();
            JArray responses = (JArray)result["responses"];
            foreach (JObject jObj in responses)
            {
                try
                {
                    var jtoken = jObj["message"];
                    int seq = (int)jtoken["sequence"];
                    string receiver = jtoken["receiver"].ToString();
                    string sender = jtoken["sender"].ToString();
                    string msg = jtoken["msg"].ToString();
                    
                    YMMessage message = new YMMessage
                    {
                        Content = msg,
                        Sender = sender,
                        Receiver = receiver
                    };

                    lst.Add(message);

                    //if (seq > YMMessagesController.Seq)
                        YMMessagesController.Seq = seq;
                }
                catch (Exception)
                {
                }
                
            }

            return lst;
        }
    }
}